<template>
  <div class="table-box">
    <ProTable ref="proTable" title="员工列表" :columns="columns" :request-api="getStaffList">
      <!-- 表格 header 按钮 -->
      <template #tableHeader>
        <el-button v-auth="'add'" type="primary" :icon="CirclePlus" @click="openDrawer('新增', defaultDta)"> 新增员工 </el-button>
      </template>
      <!-- Expand -->
      <template #expand="scope">
        {{ scope.row }}
      </template>
      <!-- 表格操作 -->
      <template #operation="scope">
        <el-button v-auth="'view'" type="primary" link :icon="View" @click="openDrawer('查看', scope.row)"> 查看 </el-button>
        <el-button v-auth="'edit'" type="primary" link :icon="EditPen" @click="openDrawer('编辑', scope.row)"> 编辑 </el-button>
        <el-button v-auth="'delete'" type="primary" link :icon="Delete" @click="deleteAccount(scope.row)"> 删除 </el-button>
      </template>
    </ProTable>
    <StaffDialog ref="dialogRef" />
  </div>
</template>

<script setup lang="ts" name="staffManager">
import { ref } from "vue";
import { Operation } from "@/api/interface";
import ProTable from "@/components/ProTable/index.vue";
import StaffDialog from "@/views/operation/components/StaffDialog.vue";
import { ProTableInstance, ColumnProps } from "@/components/ProTable/interface";
import { useHandleData } from "@/hooks/useHandleData";
import { CirclePlus, EditPen, View, Delete } from "@element-plus/icons-vue";
import { getStaffList, createStaff, editStaff, deleteStaff } from "@/api/modules/operation";
import { genderType } from "@/utils/serviceDict";

const proTable = ref<ProTableInstance>();
const defaultDta = {
  sex: 1
};
// 表格配置项
const columns: ColumnProps<Operation.ResStaffList>[] = [
  { type: "index", label: "序号", width: 60 },
  { prop: "name", label: "员工姓名", sortable: true, minWidth: 120, align: "left", search: { el: "input" } },
  { prop: "mobile", label: "手机号码", sortable: true, minWidth: 120, align: "left", search: { el: "input" } },
  {
    prop: "sex",
    label: "性别",
    sortable: true,
    minWidth: 100,
    enum: genderType,
    search: { el: "select" }
  },
  { prop: "birthday", sortable: true, label: "生日", minWidth: 164, align: "left" },
  { prop: "nickName", sortable: true, label: "昵称", minWidth: 180, align: "left" },
  { prop: "operation", label: "操作", fixed: "right", width: 220 }
];

// 删除
const deleteAccount = async (params: Operation.ResStaffList) => {
  await useHandleData(deleteStaff, { id: params.id }, `删除【${params.name}】资料`);
  proTable.value?.getTableList();
};

// 打开 drawer(新增、查看、编辑)
const dialogRef = ref<InstanceType<typeof StaffDialog> | null>(null);
const openDrawer = (title: string, row: Partial<Operation.ResStaffList> = {}) => {
  const params = {
    title,
    isView: title === "查看",
    row: { ...row },
    api: title === "新增" ? createStaff : title === "编辑" ? editStaff : undefined,
    getTableList: proTable.value?.getTableList
  };
  dialogRef.value?.acceptParams(params);
};
</script>
